package com.xiangyun.auth.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiangyun.auth.dao.TSysRoleMenuPermissionMapper;
import com.xiangyun.auth.model.RestResult;
import com.xiangyun.auth.model.TSysRoleMenuPermission;
import com.xiangyun.auth.model.TSysRoleMenuPermissionVO;
import com.xiangyun.auth.service.ServiceRoleMenuPermissionService;
import com.xiangyun.auth.utils.MapperUtils;


@Service
public class ServiceRoleMenuPermissionServiceImpl implements ServiceRoleMenuPermissionService{
	
	@Autowired
	private TSysRoleMenuPermissionMapper tSysRoleMenuPermissionMapper;

	@Override
	public String createRoleMenuPermission(TSysRoleMenuPermission tSysRoleMenuPermission) {
		// TODO Auto-generated method stub
		RestResult restResult = new RestResult();
		String json = "";
		TSysRoleMenuPermission selectByRoleMenuPermission =
				tSysRoleMenuPermissionMapper.selectByRoleMenuPermission(tSysRoleMenuPermission);
		if(null != selectByRoleMenuPermission) {
			restResult.setCode("");
			restResult.setMessage("添加失败，请重试");
			try {
				json = MapperUtils.obj2json(restResult);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return json;
		}
		Integer selective = tSysRoleMenuPermissionMapper.insertSelective(tSysRoleMenuPermission);
		if(null == selective || 0 >= selective) {
			restResult.setCode("");
			restResult.setMessage("添加失败，请重试");
			try {
				json = MapperUtils.obj2json(restResult);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return json;
		}
		restResult.setCode("0");
		restResult.setData(tSysRoleMenuPermission);
		restResult.setMessage("添加成功");
		try {
			json = MapperUtils.obj2json(restResult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}

	@Override
	public String modifyRMP(TSysRoleMenuPermission tSysRoleMenuPermission) {
		// TODO Auto-generated method stub
		RestResult restResult = new RestResult();
		String json = "";
		TSysRoleMenuPermission RMP 
		= tSysRoleMenuPermissionMapper.selectByRoleMenuPermission(tSysRoleMenuPermission);
		if(null == RMP) {
			restResult.setCode("");
			restResult.setMessage("修改失败，请重试");
			try {
				json = MapperUtils.obj2json(restResult);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return json;
		}
		Integer selective = tSysRoleMenuPermissionMapper.updateByPrimaryKeySelective(tSysRoleMenuPermission);
		if(null == selective || 0 >= selective) {
			restResult.setCode("");
			restResult.setMessage("修改失败，请重试");
			try {
				json = MapperUtils.obj2json(restResult);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return json;
		}
		restResult.setCode("0");
		restResult.setMessage("修改成功");
		restResult.setData(tSysRoleMenuPermission);
		try {
			json = MapperUtils.obj2json(restResult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}

	@Override
	public String deleteRMPById(Long id) {
		// TODO Auto-generated method stub
		RestResult restResult = new RestResult();
		String json = "";
		TSysRoleMenuPermission keyRMP = tSysRoleMenuPermissionMapper.selectByPrimaryKey(id);
		if(null == keyRMP) {
			restResult.setCode("");
			restResult.setMessage("删除失败，请重试");
			try {
				json = MapperUtils.obj2json(restResult);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return json;
		}
		Integer deleteByPrimaryKey = tSysRoleMenuPermissionMapper.deleteByPrimaryKey(id);
		if(null == deleteByPrimaryKey || 0 >= deleteByPrimaryKey) {
			restResult.setCode("");
			restResult.setMessage("删除失败，请重试");
			try {
				json = MapperUtils.obj2json(restResult);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return json;
		}
		restResult.setCode("0");
		restResult.setMessage("删除成功");
		try {
			json = MapperUtils.obj2json(restResult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}

	@Override
	public String getRMPByPage(String[] fields, String[] order, String[] sortBy, String search, Long start, Long limit,
			Long currentPage) {
		// TODO Auto-generated method stub
		RestResult restResult = new RestResult();
		String json = "";
		List<TSysRoleMenuPermissionVO> rmpByPage = 
				tSysRoleMenuPermissionMapper.getRMPByPage(start, limit, currentPage);
		if(null == rmpByPage || 0 >= rmpByPage.size()) {
			restResult.setCode("");
			restResult.setMessage("查询失败");
			try {
				json = MapperUtils.obj2json(restResult);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return json;
		}
		restResult.setCode("0");
		restResult.setMessage("查询成功");
		restResult.setData(rmpByPage);
		try {
			json = MapperUtils.obj2json(restResult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}

}
